import json
from logger import log
import traceback
from mongoengine import *
 
def to_dic(inst, cls):
    d = dict()
    for c in cls._db_field_map:
        v = getattr(inst, c)
        if  v is not None:
            try:
                d[c] = str(v)
            except:
                d[c]=str()
                log.error(traceback.format_exc())
                log.error("column "+str(c))
        elif v is None:
            d[c] = str()
        else:
            d[c] = v
    return d
 

class Track(Document):
    song_name = StringField()
    artist_name = StringField()
    play_url = StringField()
    likes = IntField()
    url = StringField()
    year=IntField()
    lyricist=StringField()
    composer=StringField()
    arranger=StringField()
    album_name=StringField()
    md5=StringField()
    def todic(self):
        return to_dic(self, self.__class__)
    
class User(Document):
    created = DateTimeField()
    user_id=StringField(unique=True)
    name = StringField()
    profile_url = StringField()
    access_token = StringField()
    facebook=IntField()
class Request(Document):
    created = DateTimeField()
    request=ReferenceField(Track) 
    user = ReferenceField(User)
    action=StringField()
class TrackLog(Document):
    created = DateTimeField()
    track=StringField()
    user = ReferenceField(User)
    action=StringField()
    position=StringField()
    show=StringField()
class Artist(Document):
    name=StringField()
    likes=IntField()
class Show(Document):
    name=StringField()
    tracks=ListField()
    name_ar=StringField()
    
    